/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.occupation.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.occupation.service.IIdentityValidateService;
import com.ce.pms.occupation.service.IInspecteRequisiteService;
import com.ce.pms.occupation.service.IPerfectItemRequisitionService;
import com.ce.pms.occupation.service.IPerfectItemTrackService;
import com.ce.pms.occupation.vo.IdentityValidateVO;
import com.ce.pms.occupation.vo.InspecteRequisiteVO;
import com.ce.pms.occupation.vo.PerfectItemRequisitionVO;
import com.ce.pms.occupation.vo.PerfectItemSearchVO;
import com.ce.pms.occupation.vo.PerfectItemTrackVO;
import com.ce.pms.system.occupation.service.IPerfectItemTypeConfigService;
import com.ce.pms.system.occupation.vo.PerfectItemTypeConfigVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

/**
 * 入伙整改项目Action
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2009-1-6
 */

public class PerfectItemRequisitionAction extends PmsBaseAction {
	/**
	 * 获取楼栋信息service
	 */
	private IBuildingService				buildingService;

	/**
	 * 详细信息
	 */
	private final String					DETAIL_PAGE			= "perfectItemRequisitionDetail";

	/**
	 * 添加或编辑页面
	 */
	private final String					EDIT_PAGE			= "perfectItemRequisitionEdit";

	/**
	 * 入伙身份验证
	 */
	private IIdentityValidateService		identityValidateService;

	/**
	 * 初始化页面
	 */
	private final String					INIT_PAGE			= "perfectItemRequisitionMain";

	/**
	 * 验房登记Service
	 */
	private IInspecteRequisiteService		inspecteRequisiteService;

	/**
	 * 列表页面
	 */
	private final String					LIST_PAGE			= "perfectItemRequisitionList";

	/**
	 * 入伙整改项目Service
	 */
	private IPerfectItemRequisitionService	perfectItemRequisitionService;

	/**
	 * 整改跟进service
	 */
	private IPerfectItemTrackService		perfectItemTrackService;

	/**
	 * 整改项目类型设置Service
	 */
	private IPerfectItemTypeConfigService	perfectItemTypeConfigService;

	/**
	 * 高级查询
	 */
	private final String					SEARCH_PAGE			= "perfectItemRequisitionSearch";

	/**
	 * 派工跟进添加或编辑页面
	 */
	private final String					TRACK_EDIT_PAGE		= "perfectItemTrackEdit";

	/**
	 * 派工跟进初始化页面
	 */
	private final String					TRACK_INIT_PAGE		= "perfectItemTrackMain";

	/**
	 * 整改跟进详细
	 */
	private final String					TRACK_DETAIL_PAGE	= "perfectItemTrackDetail";

	/**
	 * 派工跟进列表页面
	 */
	private final String					TRACK_LIST_PAGE		= "perfectItemTrackList";

	public void deletePerfectItemRequisition(@Read(key = "keyword") Long keyword) {
		try {
			this.perfectItemRequisitionService
					.deletePerfectItemRequisition(keyword);
			this.renderText("true");
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 删除派工跟进
	 *
	 * @param keyword
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	public void deletePerfectItemTrack(@Read(key = "keyword") Long keyword) {
		try {
			this.perfectItemTrackService.deletePerfectItemTrack(keyword);
			this.renderText("true");
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 详细页面
	 *
	 * @param keyword 主键
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	public String detailPage(@Read(key = "keyword") Long keyword) {
		PerfectItemRequisitionVO perfectItemRequisitionVO = this.perfectItemRequisitionService
				.queryPerfectItemRequisitionVOById(keyword);
		this.setAttribute("perfectItemRequisitionVO", perfectItemRequisitionVO);
		return this.DETAIL_PAGE;
	}

	/**
	 * 添加或编辑页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	public String editPage(@Read(key = "keyword") Long keyword) {
		if (keyword != null) {
			PerfectItemRequisitionVO perfectItemRequisitionVO = this.perfectItemRequisitionService
					.queryPerfectItemRequisitionVOById(keyword);
			this.setAttribute("perfectItemRequisitionVO",
					perfectItemRequisitionVO);
		}

		// 申报项目类型
		List<PerfectItemTypeConfigVO> perfectItemTypeList = this.perfectItemTypeConfigService
				.queryAllPerfectItemTypeConfig();
		this.setAttribute("perfectItemTypeList", perfectItemTypeList);

		return this.EDIT_PAGE;
	}

	/**
	 * 添加编辑整改项目
	 *
	 * @param perfectItemRequisitionVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	public void editPerfectItemRequisition(
			@Read PerfectItemRequisitionVO perfectItemRequisitionVO) {
		perfectItemRequisitionVO
				.setJoinMode("002".equals(perfectItemRequisitionVO
						.getJoinMode()) ? "002" : "001");

		try {
			if (BlankUtil.isBlank(perfectItemRequisitionVO.getKeyword())) {
				this.perfectItemRequisitionService
						.addPerfectItemRequisition(perfectItemRequisitionVO);
			} else {
				this.perfectItemRequisitionService
						.editPerfectItemRequisition(perfectItemRequisitionVO);
			}
			this.renderText("true");
		} catch (RuntimeException re) {
			throw re;
		}

	}

	/**
	 * 添加或编辑整改更进
	 *
	 * @param perfectItemTrackVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	public void editPerfectItemTrack(@Read PerfectItemTrackVO perfectItemTrackVO) {
		try {
			if (BlankUtil.isBlank(perfectItemTrackVO.getKeyword())) {
				this.perfectItemTrackService
						.addPerfectItemTrack(perfectItemTrackVO);
			} else {
				this.perfectItemTrackService
						.editPerfectItemTrack(perfectItemTrackVO);
				this.renderText("true");
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-9
	 */
	public void getHouseInfo() {

	}

	public void getIdentityValidate(@Read(key = "houseId") Long houseId) {

		// //拿管理房
		// CoalitionHouseDomain coalitionHouseDomain
		// =houseService.getCoalition(houseId);
		//
		// if(BlankUtil.isBlank(coalitionHouseDomain)){//不是管理房
		// HouseDomain house = houseService.getHouseById(houseId);
		// List<ParticipantDO> list =
		// clientRHouseService.getAllOwnerByHouseId(house.getKeyword());
		// for(ParticipantDO participantDO : list) {
		// Object[] obj = new Object[6];
		// obj[0] = house.getKeyword();
		// obj[1] = house.getHouseNumber();
		// }
		// }

		IdentityValidateVO identityValidateVO = this.identityValidateService
				.findIdentityValidateVOByHouseId(houseId);
		if (BlankUtil.isBlank(identityValidateVO)) {
			throw new BusinessException(
					"occupation:occupation_perfectItemRequisition_add_exception",
					BusinessException.WARNING);
		} else {
			List<InspecteRequisiteVO> inspecteRequisiteVOList = this.inspecteRequisiteService
					.queryInspecteRequisiteByIdentityValidateId(identityValidateVO
							.getKeyword());
			if (BlankUtil.isBlank(inspecteRequisiteVOList)) {
				// 验房id
				throw new BusinessException(
						"occupation:occupation_perfectItemRequisition_add_exception",
						BusinessException.WARNING);
			} else {
				Long inspectionRequisitionId = inspecteRequisiteVOList.get(0)
						.getKeyword();
				this.renderText(inspectionRequisitionId.toString());
			}
		}

		// (identityValidateVO.getKeyword());

		// List<InspecteRequisiteVO> inspecteRequisiteVOList =
		// this.inspecteRequisiteService
		// .queryInspecteRequisiteByHouseId(houseId);
		//
		// if (!(BlankUtil.isBlank(inspecteRequisiteVOList) ||
		// inspecteRequisiteVOList
		// .isEmpty())) {
		// InspecteRequisiteVO inspecteRequisiteVO = inspecteRequisiteVOList
		// .get(0);
		//
		// IdentityValidateVO identityValidateVO
		// =inspecteRequisiteVO.getIdentityValidate();
		//
		//
		// }

		// List<InspecteRequisiteVO> inspecteRequisiteVOList =
		// this.inspecteRequisiteService
		// .queryInspecteRequisiteByHouseId(houseId);
		// try {
		// if (BlankUtil.isBlank(inspecteRequisiteVOList)
		// || inspecteRequisiteVOList.isEmpty()) {
		// throw new RuntimeException("该房间无验房记录！");
		// } else {
		// InspecteRequisiteVO inspecteRequisiteVO = inspecteRequisiteVOList
		// .get(0);
		// String jsoneTmp = "{}";
		// }
		// } catch (RuntimeException re) {
		// throw re;
		// }
	}

	/**
	 * 初始化页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-6
	 */
	public String initPage() {
		// 房屋列表
		List<BuildingVO> buildingList = this.buildingService
				.getAllBuildingList();
		this.setAttribute("buildingList", buildingList);

		// 申报项目类型
		List<PerfectItemTypeConfigVO> perfectItemTypeList = this.perfectItemTypeConfigService
				.queryAllPerfectItemTypeConfig();
		this.setAttribute("perfectItemTypeList", perfectItemTypeList);

		return this.INIT_PAGE;
	}

	/**
	 * 列表初始化页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	public String listPage() {
		this.setAttribute("totalRows", new Integer(0));
		return this.LIST_PAGE;
	}

	/**
	 * 分页查找结果
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	@SuppressWarnings("unchecked")
	public String queryPerfectItemRequisitionByPage(
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd", defaultValue = "15") Integer pageSize,
			@Read(key = "overflowHight", defaultValue = "") @Out(key = "overflowHight") String overflowHight) {
		PerfectItemSearchVO perfectItemSearchVO = (PerfectItemSearchVO) this
				.getAtrributeFromSeesion("perfectItemSearchVO");
		perfectItemSearchVO.setPageNo(pageNO);
		perfectItemSearchVO.setPageSize(pageSize);

		perfectItemSearchVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));

		if (perfectItemSearchVO != null) {
			Page page = this.perfectItemRequisitionService
					.queryPerfectItemRequisitionVOByPage(perfectItemSearchVO);
			List<PerfectItemRequisitionVO> perfectItemRequisitionList = page
					.getResult();
			// TODO 显示处理逻辑

			this.setAttribute("queryDataList", perfectItemRequisitionList);
			this.setAttribute("totalRows", Long.valueOf(page.getTotalCount())
					.intValue());
		} else {
			this.setAttribute("totalRows", new Integer(0));
		}
		return this.LIST_PAGE;
	}

	/**
	 * 查询整改项目列表
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-6
	 */
	public String queryPerfectItemRequisitionList() {
		this.setAttribute("totalRows", new Integer(0));
		return this.LIST_PAGE;
	}

	/**
	 * 高级搜索
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	public String searchPage() {
		// 房屋列表
		List<BuildingVO> buildingList = this.buildingService
				.getAllBuildingList();
		this.setAttribute("buildingList", buildingList);

		// 申报项目类型
		List<PerfectItemTypeConfigVO> perfectItemTypeList = this.perfectItemTypeConfigService
				.queryAllPerfectItemTypeConfig();
		this.setAttribute("perfectItemTypeList", perfectItemTypeList);

		return this.SEARCH_PAGE;
	}

	/**
	 * @param buildingService the buildingService to set
	 */
	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * @param identityValidateService the identityValidateService to set
	 */
	public void setIdentityValidateService(
			IIdentityValidateService identityValidateService) {
		this.identityValidateService = identityValidateService;
	}

	/**
	 * @param inspecteRequisiteService the inspecteRequisiteService to set
	 */
	public void setInspecteRequisiteService(
			IInspecteRequisiteService inspecteRequisiteService) {
		this.inspecteRequisiteService = inspecteRequisiteService;
	}

	/**
	 * @param perfectItemRequisitionService the perfectItemRequisitionService to
	 *            set
	 */
	public void setPerfectItemRequisitionService(
			IPerfectItemRequisitionService perfectItemRequisitionService) {
		this.perfectItemRequisitionService = perfectItemRequisitionService;
	}

	/**
	 * @param perfectItemTrackService the perfectItemTrackService to set
	 */
	public void setPerfectItemTrackService(
			IPerfectItemTrackService perfectItemTrackService) {
		this.perfectItemTrackService = perfectItemTrackService;
	}

	/**
	 * @param perfectItemTypeConfigService the perfectItemTypeConfigService to
	 *            set
	 */
	public void setPerfectItemTypeConfigService(
			IPerfectItemTypeConfigService perfectItemTypeConfigService) {
		this.perfectItemTypeConfigService = perfectItemTypeConfigService;
	}

	/**
	 * 将结果保存在session
	 *
	 * @param perfectItemSearchVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	public void setSearchCatch(@Read PerfectItemSearchVO perfectItemSearchVO) {
		this.setAttribute("perfectItemSearchVO", perfectItemSearchVO,
				Scope.SESSION);
		this.renderText("true");
	}

	/**
	 * 派工跟进添加或编辑页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	public String trackEditPage(
			@Read(key = "perfectItemRequisitionId") @Out(key = "perfectItemRequisitionId") Long perfectItemRequisitionId,
			@Read(key = "keyword") Long keyword) {
		;
		if (keyword != null) {
			PerfectItemTrackVO perfectItemTrackVO = this.perfectItemTrackService
					.queryPerfectItemTrackVOById(keyword);
			this.setAttribute("perfectItemTrackVO", perfectItemTrackVO);
		}

		return this.TRACK_EDIT_PAGE;
	}

	public String trackDetailPage(@Read(key = "keyword") Long keyword) {
		this.perfectItemTrackService.queryPerfectItemTrackVOById(keyword);
		return TRACK_DETAIL_PAGE;
	}

	/**
	 * 派工跟进初始化页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	public String trackInitPage(
			@Read(key = "keyword") @Out(key = "perfectItemRequisitionId") Long keyword,
			@Read(key = "hasToolbar") @Out(key = "hasToolbar") boolean hasToolbar) {
		PerfectItemRequisitionVO perfectItemRequisitionVO = this.perfectItemRequisitionService
				.queryPerfectItemRequisitionVOById(keyword);
		this.setAttribute("perfectItemRequisitionVO", perfectItemRequisitionVO);
		return this.TRACK_INIT_PAGE;
	}

	/**
	 * 办理模式
	 *
	 * @param houseId
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-16
	 */
	public void isManager(@Read(key = "houseId") Long houseId) {
		IdentityValidateVO identityValidateVO = identityValidateService
				.findIdentityValidateVOByHouseId(houseId);
		this.renderText(identityValidateVO.getJoinMode());
	}

	/**
	 * 派工单列表
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-7
	 */
	@SuppressWarnings("unchecked")
	public String trackListPage(@Read(key = "keyword")@Out(key="trackId") Long keyword,
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd", defaultValue = "15") Integer pageSize) {

		PerfectItemTrackVO perfectItemTrackVO = new PerfectItemTrackVO();
		perfectItemTrackVO.setPerfectItemRequisitionId(keyword);
		perfectItemTrackVO.setPageNo(pageNO);
		perfectItemTrackVO.setPageSize(pageSize);

		Page page = this.perfectItemTrackService
				.queryPerfectItemTrackVOByPage(perfectItemTrackVO);

		List<PerfectItemTrackVO> tmpList = page.getResult();
		if (!BlankUtil.isBlank(tmpList)) {
			List<PerfectItemTrackVO> perfectItemTrackVOList = new ArrayList<PerfectItemTrackVO>();

			// 处理查询结果
			for (PerfectItemTrackVO tmpVO : tmpList) {
				tmpVO.setCompleteYn("1".equals(tmpVO.getCompleteYn()) ? "完工"
						: "未完工");
				perfectItemTrackVOList.add(tmpVO);
			}

			this.setAttribute("queryDataList", perfectItemTrackVOList);
			this.setAttribute("totalRows", Long.valueOf(page.getTotalCount())
					.intValue());

		} else {
			this.setAttribute("totalRows", new Integer(0));
		}
		return this.TRACK_LIST_PAGE;
	}

	/**
	 * 判断整改跟进的日期
	 *
	 * @param keyword 整改项目的日期
	 * @param perfectDate 整改跟进的日期
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-21
	 */
	public void hasTrackDate(@Read(key = "keyword") Long keyword,
			@Read(key = "trackDate") Date trackDate) {
		PerfectItemRequisitionVO perfectItemRequisitionVO = perfectItemRequisitionService
				.queryPerfectItemRequisitionVOById(keyword);

		Date perfectRequisitionDate = perfectItemRequisitionVO
				.getPerfectRequisitionDate();
		if (perfectRequisitionDate.compareTo(trackDate) > 0) {
			this.renderText("跟进时间要大于派工日期");
		} else {
			this.renderText("true");
		}
	}

	/**
	 * 是否完工
	 *
	 * @param keyword
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-21
	 */
	public void hasTrackState(@Read(key = "keyword") Long keyword) {
		try {
			this.perfectItemTrackService.checkEditIsTrue(keyword);
			renderText("true");
		} catch (RuntimeException re) {
			throw re;
		}
	}

}
